package com.mhys.hms.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhys.hms.entity.Department;
import com.mhys.hms.entity.FeedBackObject;
import com.mhys.hms.service.user.DeptManagerService;

/**
 * 部门管理controller
 * @author 
 *
 */
@Controller
@Scope(value="prototype")
//@RequestMapping(value="/queryDeptInfo")
public class DeptManager {
	@Autowired
	private DeptManagerService deptManagerService;
	
	/**
	 * 分页模糊查询
	 * @param pageSize
	 * @param pageNumber
	 * @param deptNo
	 * @param deptName
	 * @return
	 */
	@RequestMapping(value="/queryDeptInfo",method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public FeedBackObject queryDeptInfo(int pageSize,int pageNumber,String deptNo,String deptName) {
		FeedBackObject fbo = new FeedBackObject();
		int num=1;
		int num2=2;
		//分页查询数据
		List<Department> list = deptManagerService.getDeptInfo(pageSize,pageNumber,deptNo,deptName);
		//封装数据
		Map<String,Object> map = new HashMap<String,Object>();
		//返回总页数
		map.put("total", deptManagerService.getTotalNum(pageSize, pageNumber, deptNo, deptName));
		//返回总数据
		map.put("data", list);
		fbo.Obj = map;
		return fbo;
	}
	/**
	 * 根据id查询部门信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/queryDeptInfoById",method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public FeedBackObject queryDeptInfoById(int id) {
		FeedBackObject fbo = new FeedBackObject();
		
		//分页查询数据
		List<Department> list = deptManagerService.getDeptInfoById(id);
		//封装数据
		Map<String,Object> map = new HashMap<String,Object>();
		//返回总页数
		map.put("total", 1);
		//返回总数据
		map.put("data", list);
		fbo.Obj = map;
		return fbo;
	}
	/**
	 * 根据id删除部门
	 * @param deptId 部门id
	 * @return
	 */
	@RequestMapping(value="/delDeptInfo",method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public FeedBackObject delDeptInfo(String deptId) {
		FeedBackObject fbo = new FeedBackObject();
		Integer row = deptManagerService.delDeptInfo(deptId);
		if(row>0) {
			fbo.success = "OK";
		}else if(row==-1) {
			fbo.msg = "该部门存在员工，不能删除。";	
		}else {
			fbo.msg = "网络异常，请稍后重试。";
		}
		return fbo;
	}
	/**
	 * 新增部门信息
	 * @param dept 新增的部门信息
	 * @return
	 */
	@RequestMapping(value="/addDeptInfo",method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public FeedBackObject addDeptInfo(String departmentName,Date createTime,
			String personNum,String remark) {
		FeedBackObject fbo = new FeedBackObject();
		Department dept = new Department();
		dept.setCreateTime(createTime);
		dept.setDepartmentName(departmentName);
		dept.setPersonNum(Integer.parseInt(personNum));
		dept.setRemark(remark);
		Integer row = deptManagerService.addDeptInfo(dept);
		if(row>0) {
			fbo.success = "OK";
		}else if(row==-1){
			fbo.success = "fail";
			fbo.msg = "部门名已经存在，请重新输入。";
		}else {
			fbo.success = "fail";
			fbo.msg = "服务器繁忙，请稍后再试。";
		}
		return fbo;
	}
	/**
	 * 根据部门id更新部门信息
	 * @param id 部门id
	 * @param departmentName 部门名称
	 * @param createTime 创建时间
	 * @param personNum 部门人数
	 * @param remark 备注
	 * @return
	 */
	@RequestMapping(value="/updateDeptInfo",method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public FeedBackObject updateDeptInfo(Integer id,String departmentName,Date createTime,
			String personNum,String remark) {
		FeedBackObject fbo = new FeedBackObject();
		Department dept = new Department();
		dept.setId(id);
		dept.setCreateTime(createTime);
		dept.setDepartmentName(departmentName);
		dept.setPersonNum(Integer.parseInt(personNum));
		dept.setRemark(remark);
		Integer row = deptManagerService.addDeptInfo(dept);
		if(row>0) {
			fbo.success = "OK";
		}else {
			fbo.success = "fail";
		}
		return fbo;
	}
	/**
	 * 查询所有部门信息
	 * @return
	 */
	@RequestMapping(value="/queryAllDeptName",method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public FeedBackObject queryAllDeptName() {
		FeedBackObject fbo = new FeedBackObject();
		List<String> list = deptManagerService.queryAllDeptName();
		fbo.Obj = list;
		return fbo;
	}
}
